﻿using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace GameOfLife
{
    public partial class MainPage : ContentPage
    {
        private const int CellSize = 20; // Size of each cell
        private bool[,] grid;
        private int rows, columns;
        //private Canvas canvas;

        public MainPage()
        {
            InitializeComponent();
            rows = LifeGrid.RowDefinitions.Count;
            columns = LifeGrid.ColumnDefinitions.Count;
            grid = new bool[rows, columns];
            InitializeGrid();
        }

        private void InitializeGrid()
        {
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    LifeGrid.Children.Add(new BoxView
                    {
                        Color = grid[i, j] ? Colors.Black : Colors.White,
                        WidthRequest = CellSize,
                        HeightRequest = CellSize,
                        TranslationX = j * CellSize,
                        TranslationY = i * CellSize
                    });
                }
            }
        }

        private void OnTapGestureRecognizerTapped(object sender, EventArgs e)
        {
            Point point = e.ChosenObject.TranslateToParent(LifeGrid);
            int row = (int)point.Y / CellSize;
            int column = (int)point.X / CellSize;

            if (row >= 0 && row < rows && column >= 0 && column < columns)
            {
                grid[row, column] = !grid[row, column];
                var boxView = (BoxView)LifeGrid.Children[row * columns + column];
                boxView.Color = grid[row, column] ? Colors.Black : Colors.White;
            }
        }

        private async void StartSimulation()
        {
            while (true)
            {
                grid = GetNextGeneration(grid);
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        var boxView = (BoxView)LifeGrid.Children[i * columns + j];
                        boxView.Color = grid[i, j] ? Colors.Black : Colors.White;
                    }
                }
                await Task.Delay(500); // Delay for animation effect
            }
        }

        private bool[,] GetNextGeneration(bool[,] currentGrid)
        {
            bool[,] newGrid = new bool[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    int aliveNeighbors = CountAliveNeighbors(currentGrid, i, j);
                    if (currentGrid[i, j])
                    {
                        newGrid[i, j] = aliveNeighbors == 2 || aliveNeighbors == 3;
                    }
                    else
                    {
                        newGrid[i, j] = aliveNeighbors == 3;
                    }
                }
            }
            return newGrid;
        }

        private int CountAliveNeighbors(bool[,] grid, int row, int column)
        {
            return grid[row - 1, column - 1] + grid[row - 1, column] + grid[row - 1, column + 1] +
                   grid[row, column - 1] + grid[row, column + 1] +
                   grid[row + 1, column - 1] + grid[row + 1, column] + grid[row + 1, column + 1];
        }
    }
}